<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="职位名称" prop="jobName">
        <el-input
          v-model="queryParams.jobName"
          placeholder="请输入职位名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="职位类型" prop="jobType">
        <el-select
          v-model="queryParams.jobType"
          placeholder="请选择职位类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.JOB_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="最低薪资" prop="salaryRangeFrom">
        <el-input
          v-model="queryParams.salaryRangeFrom"
          placeholder="请输入最低薪资"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="最高薪资" prop="salaryRangeTo">
        <el-input
          v-model="queryParams.salaryRangeTo"
          placeholder="请输入最高薪资"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="工作城市" prop="jobCity">
        <el-cascader
          class="popper-select"
          v-model="queryParams.jobCity"
          :options="cityTree"
          :props="optionProps"
          clearable
        />
      </el-form-item>
      <el-form-item label="学历" prop="jobRequireEdu">
        <el-select
          v-model="queryParams.jobRequireEdu"
          placeholder="请选择学历"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.JOB_REQUIRE_EDU)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="mt-24">
      <el-row :gutter="20">
        <el-col v-for="(item, index) in list" :key="index" :span="6">
          <el-card class="mb-8" shadow="hover">
            <template #header>
              <div class="card-header" style="text-align: left">
                <span>{{ item.jobName }}</span>
                <span class="salary" v-if="item.salaryMeet == 1">面议</span>
                <div v-else class="salary">
                  <span v-if="item.salaryRangeFrom">{{ item.salaryRangeFrom }}K</span>
                  <span v-else-if="item.salaryRangeTo">至{{ item.salaryRangeTo }}K</span>
                  <span v-else-if="item.salaryMonth">*{{ item.salaryMonth }}薪</span>
                </div>
              </div>
            </template>
            <div>
              <el-tag style="margin-right: 10px">{{ item.jobCity }}</el-tag>
              <dict-tag :type="DICT_TYPE.JOB_TYPE" :value="item.jobType" />

              <div class="avatar">
                <el-avatar :src="item.avatarPath" />
                <span class="avatar-span">{{ item.enterpriseName }}</span>
              </div>
            </div>
            <template #footer>
              <div style="text-align: left">
                <el-button link type="primary" @click="openForm(item.jobId)"> 职位详情 </el-button>
              </div>
            </template>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <JobInfoForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { JobInfoApi, JobInfoVO } from '@/api/dbit/jobinfo'
import JobInfoForm from './JobInfoForm1.vue'

/** 企业职位信息 列表 */
defineOptions({ name: 'JobInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<JobInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const cityTree = ref([])
// 级联下拉配置
const optionProps = {
  value: 'id',
  label: 'name',
  children: 'children',
  multiple: false,
  emitPath: false
}
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  jobName: undefined,
  jobType: undefined,
  jobCity: undefined,
  jobRequireEdu: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await JobInfoApi.getJobInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (id: number) => {
  formRef.value.open(id)
}

/** 获取城市列表 */
const getCityList = async () => {
  const c = await JobInfoApi.getCityTree()
  // 只获取城市和省份，县级去除
  c.forEach((item: any) => {
    if (item.children.length > 0) {
      item.children.map((j: any) => {
        j.children = undefined
      })
    }
  })
  cityTree.value = c
}

/** 初始化 **/
onMounted(() => {
  getList()
  getCityList()
})
</script>
<style lang="css">
.salary {
  float: right;
  font-size: 15px;
  color: #f77b72;
}

.popper-select {
  li[aria-haspopup='true'] {
    .el-checkbox {
      display: none;
    }
  }
}

.avatar {
  display: flex;
  align-items: center; /* 垂直居中对齐 */
  margin-top: 30px;
}

.avatar-span {
  margin-left: 15px;
}
</style>
